package cn.iocoder.yudao.module.peizhen.service.hospital;

import cn.iocoder.yudao.module.peizhen.controller.app.hospital.vo.AppHospitalPageReqVO;
import cn.iocoder.yudao.module.peizhen.dal.dataobject.hospital.SectionDO;
import cn.iocoder.yudao.module.peizhen.dal.dataobject.opencity.OpenCityDO;
import cn.iocoder.yudao.module.peizhen.dal.mysql.hospital.SectionMapper;
import cn.iocoder.yudao.module.peizhen.service.opencity.OpenCityService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.peizhen.controller.admin.hospital.vo.*;
import cn.iocoder.yudao.module.peizhen.dal.dataobject.hospital.HospitalDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.peizhen.dal.mysql.hospital.HospitalMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.peizhen.enums.ErrorCodeConstants.*;

/**
 * 医院 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class HospitalServiceImpl implements HospitalService {

    @Resource
    private HospitalMapper hospitalMapper;
    @Resource
    private OpenCityService openCityService;
    @Resource
    private SectionMapper sectionMapper;

    @Override
    public Integer createHospital(HospitalSaveReqVO createReqVO) {
        // 插入
        OpenCityDO openCity = openCityService.getOpenCity(createReqVO.getCityCode());
        OpenCityDO province = openCityService.getOpenCity(openCity.getParentId());
        createReqVO.setCity(openCity.getName());
        createReqVO.setProvince(province.getName());
        createReqVO.setProvinceCode(province.getId());
        HospitalDO hospital = BeanUtils.toBean(createReqVO, HospitalDO.class);
        hospitalMapper.insert(hospital);
        // 返回
        return hospital.getId();
    }

    @Override
    public void updateHospital(HospitalSaveReqVO updateReqVO) {
        // 校验存在
        validateHospitalExists(updateReqVO.getId());
        // 更新
        HospitalDO updateObj = BeanUtils.toBean(updateReqVO, HospitalDO.class);
        hospitalMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHospital(Integer id) {
        // 校验存在
        validateHospitalExists(id);
        // 删除
        hospitalMapper.deleteById(id);

        // 删除子表
        deleteSectionByHospitalId(id);
    }

    private void validateHospitalExists(Integer id) {
        if (hospitalMapper.selectById(id) == null) {
            throw exception(HOSPITAL_NOT_EXISTS);
        }
    }

    @Override
    public HospitalDO getHospital(Integer id) {
        return hospitalMapper.selectById(id);
    }

    @Override
    public PageResult<HospitalDO> getHospitalPage(HospitalPageReqVO pageReqVO) {
        return hospitalMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（医院科室） ====================

    @Override
    public PageResult<SectionDO> getSectionPage(PageParam pageReqVO, Integer hospitalId) {
        return sectionMapper.selectPage(pageReqVO, hospitalId);
    }

    @Override
    public Integer createSection(SectionDO section) {
        sectionMapper.insert(section);
        return section.getId();
    }

    @Override
    public void updateSection(SectionDO section) {
        // 校验存在
        validateSectionExists(section.getId());
        // 更新
        sectionMapper.updateById(section);
    }

    @Override
    public void deleteSection(Integer id) {
        // 校验存在
        validateSectionExists(id);
        // 删除
        sectionMapper.deleteById(id);
    }

    @Override
    public SectionDO getSection(Integer id) {
        return sectionMapper.selectById(id);
    }

    private void validateSectionExists(Integer id) {
        if (sectionMapper.selectById(id) == null) {
            throw exception(SECTION_NOT_EXISTS);
        }
    }

    private void deleteSectionByHospitalId(Integer hospitalId) {
        sectionMapper.deleteByHospitalId(hospitalId);
    }


//    app
@Override
public PageResult<HospitalDO> getHospitalPage(AppHospitalPageReqVO pageReqVO) {
    return hospitalMapper.selectPage(pageReqVO);
}
}